﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;

namespace PlanningDepartment
{
    public partial class FrKiemKe : Telerik.WinControls.UI.RadForm
    {
        public FrKiemKe()
        {
            InitializeComponent();
        }
        private SqlConnection Conn;
        private Process prs = new Process();

        private DataSet ds = new DataSet();
        private string string_temp = null, child = " - ",Serial1, MaSP1, MaCT1, Datebegin, Dateend, GhiChu1, MaKH1, TenKH1, KhuVuc1, ID, TenCT1, TenSP1,CDay1,CRong1,CDai1,SoLuong1;
        private int i = 0,int_temp=0;
        private void Load_()
        {
            SqlCommand cmd = new SqlCommand();
            Conn = prs.Connect_DBScansia();
            Conn.Open();
            DataSet ds = new DataSet();
            try
            {

                cmd.CommandText = "select MaSP,TenSP from dbo.Load_cbSP";
                cmd.Connection = Conn;
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    cb_SanPham.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_MaSP_edit.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                }

                rd.Close();
                string_temp = @"select dbo.Load_CTSP_Mua.MaSP,dbo.Load_CTSP_Mua.TenChiTiet+' - '+dbo.Load_CTSP_Mua.MaChiTiet,ChieuDaySC,ChieuRongSC,ChieuDaiSC from dbo.Load_CTSP_Mua
                                group by dbo.Load_CTSP_Mua.MaSP,dbo.Load_CTSP_Mua.TenChiTiet+' - '+dbo.Load_CTSP_Mua.MaChiTiet,ChieuDaySC,ChieuRongSC,ChieuDaiSC";
                ds = prs.dts(string_temp, Conn, ds);
                dataGridView_LoadSP.DataSource = ds.Tables[0];
                string[] arr_LoaiGo = new string[] { "KV1 Ripsaw", "KV1 Tan Dung", "KV1 Long", "KV1 Uon", "KV1 Cat KT", "KV1 Cat Hoi", "KV1 Xe", "KV1 Truoc Bao","KV1 Sau Bao","KV1 Cha Nham","KV1 Tram Tret","KV1 KTG","Dinh Vi","KV1 Ghep","KV1 KTG","KV2 Dinh Vi","KV2 Tram Tret","KV2 Tan Dung","KV2 CNC","KV2 Tai Che","KV3 Kho Cho Rap","KV3 Lap Rap","KV3 Kho Tam","KV4 Sau Nhung","KV4 Xuong Chuyen","KV4 Tan Dung","KV4 Tai Che","KV5 Dong Goi" };
                for (i = 0; i < arr_LoaiGo.Length; i++)
                {

                    cb_KhuVuc.Items.Add(arr_LoaiGo[i].ToString());
                    cb_KhuVuc_edit.Items.Add(arr_LoaiGo[i].ToString());
                    cb_KhuVucBK.Items.Add(arr_LoaiGo[i].ToString());
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                
                cmd.Dispose();
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TenCT.Text != "")
            {
                for (i = 0; i < dataGridView_LoadSP.Rows.Count - 1; i++)
                {
                    if (cb_TenCT.Text == dataGridView_LoadSP.Rows[i].Cells[1].Value.ToString())
                    {
                        txt_CDay.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuDaySC"].Value.ToString();
                        txt_CRong.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuRongSC"].Value.ToString();
                        txt_CDai.Text = dataGridView_LoadSP.Rows[i].Cells["ChieuDaiSC"].Value.ToString();

                        break;
                    }
                }

            }
        }

        private void FrKiemKe_Load(object sender, EventArgs e)
        {
            Load_();
        }

        private void cb_SanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaSP1 = prs.substring_right(cb_SanPham.Text, child);
            cb_TenCT.Items.Clear();

            cb_TenCT.Text = "";
            for (i = dataGridView_LoadSP.Rows.Count - 2; i >= 0; i--)
            {
                if (MaSP1 == dataGridView_LoadSP.Rows[i].Cells[0].Value.ToString())
                {
                    cb_TenCT.Items.Add(dataGridView_LoadSP.Rows[i].Cells[1].Value.ToString());
                }
            }
        }

        private void FrKiemKe_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyData == Keys.PageDown)
            {
                bt_add_input_Click(null, null);
            }
            if (ex.KeyData == Keys.F11)
            {
                bt_save_input_Click(null, null);
            }
        }
        private void add_grid()
        {
            try
            {
                if (cb_KhuVuc.Text == "" || txt_SoLuong.Text == "" || txt_CDay.Text == "" || txt_CRong.Text == "" || txt_CDai.Text == "")
                {
                    MessageBox.Show("Nhập thiếu thông tin");
                }
                else
                {
                    TenSP1 = "";
                    TenCT1 = "";
                    MaCT1 = "";
                    if (cb_SanPham.Text != "")
                    {
                        TenSP1 = prs.substring_left(cb_SanPham.Text, child);
                       
                        
                        
                    }
                    if (cb_TenCT.Text != "")
                    {
                        MaCT1 = prs.substring_right(cb_TenCT.Text, child);
                        TenCT1 = prs.substring_left(cb_TenCT.Text, child);
                    }
                    
                    int_temp = radGridView1.Rows.Count;
                    if (int_temp > 0)
                    {
                        i = int.Parse(radGridView1.Rows[int_temp - 1].Cells["STT"].Value.ToString()) + 1;

                        radGridView1.Rows.Add(i.ToString(), TenSP1, MaCT1, TenCT1, txt_SoLuong.Text.Trim(), txt_CDay.Text.Trim(), txt_CRong.Text.Trim(), txt_CDai.Text.Trim(), txt_GhiChu.Text.Trim());
                        //if (i % 2 == 0)
                        //  e.RowElement.BackColor = System.Drawing.Color.AliceBlue;
                    }
                    else
                    {
                        radGridView1.Rows.Add("1", TenSP1, MaCT1, TenCT1, txt_SoLuong.Text.Trim(), txt_CDay.Text.Trim(), txt_CRong.Text.Trim(), txt_CDai.Text.Trim(), txt_GhiChu.Text.Trim());
                    }
                    //cb_SanPham.Text = "";
                    bt_rework_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView1);
        }

        private void bt_add_input_Click(object sender, EventArgs e)
        {
            add_grid();
        }

        private void bt_rework_Click(object sender, EventArgs e)
        {
            cb_TenCT.Text = "";
            txt_SoLuong.Text = "";
            txt_GhiChu.Text = "";
            txt_CDai.Text = "";
            txt_CRong.Text = "";
            txt_CDay.Text = "";
            cb_SanPham.Focus();
        }

        private void radGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                try
                {
                    //MessageBox.Show(radGridView1.Rows.Count.ToString());
                    int r = radGridView1.CurrentRow.Index;
                    radGridView1.Rows.RemoveAt(r);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void save()
        {
            if (radGridView1.Rows.Count > 0)
            {
                Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();
                Conn.Open();
                cmd.Connection = Conn;

                
                try
                {
                    KhuVuc1 = cb_KhuVuc.Text.Trim();
                    
                    string[] temp = txt_Date.Text.Split('/');
                    Datebegin = temp[1] + "/" + temp[0] + "/" + temp[2];
                    while (radGridView1.Rows.Count > 0)
                    {
                        //TenSP1 = radGridView1.Rows[""].Cells[""].Value.ToString();
                        TenSP1 = radGridView1.Rows[0].Cells["TenSP"].Value.ToString();
                        MaCT1 = radGridView1.Rows[0].Cells["MaCT"].Value.ToString();
                        CDay1 = radGridView1.Rows[0].Cells["CDay"].Value.ToString();
                        CRong1 = radGridView1.Rows[0].Cells["CRong"].Value.ToString();
                        CDai1 = radGridView1.Rows[0].Cells["CDai"].Value.ToString();
                        SoLuong1 = radGridView1.Rows[0].Cells["SoLuong"].Value.ToString();
                        GhiChu1 = radGridView1.Rows[0].Cells["GhiChu"].Value.ToString();
                        string_temp = @"INSERT INTO [KiemKe](TenSP,[MaCT],[CDay],[CRong],[CDai],[SoLuong],[KhuVuc],[GhiChu],[Ngay]) VALUES (N'"+TenSP1+"','"+MaCT1 + "'," + CDay1 + "," + CRong1 + "," + CDai1 + "," + SoLuong1 + ",'" + KhuVuc1 + "',N'" + GhiChu1 + "','" + Datebegin + "')";

                        cmd.CommandText = string_temp;
                        cmd.ExecuteNonQuery();
                        radGridView1.Rows.RemoveAt(0);
                        
                        
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_rework_Click(null, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void BangKe()
        {
           // Process prs = new Process();
            Conn = prs.Connect();
            Conn.Open();
            string[] temp = dateTimePicker1.Text.Split('/');
            Datebegin = temp[1] + "/" + temp[0] + "/" + temp[2];
            temp.Clone();
            string[] temp1 = dateTimePicker2.Text.Split('/');
            Dateend = temp1[1] + "/" + temp1[0] + "/" + temp1[2];
            temp1.Clone();
            string_temp = @"SELECT [Serial],Load_CTSP_Mua.TenSP,Load_CTSP_Mua.TenChiTiet as TenCT,[MaCT],[CDay],[CRong],[CDai],[SoLuong],[KhuVuc],[GhiChu],[Ngay]
                        FROM [Planning].[dbo].[KiemKe] left join DataBase_ScansiaPacific2013.dbo.Load_CTSP_Mua on [Planning].[dbo].[KiemKe].MaCT=DataBase_ScansiaPacific2013.dbo.Load_CTSP_Mua.MaChiTiet
                        where Ngay>='"+Datebegin+" 00:00' and Ngay<='"+Dateend+" 23:59'";
            if (cb_KhuVucBK.Text != "")
                string_temp += " and KhuVuc='" + cb_KhuVucBK.Text + "'";
            DataSet ds = new DataSet();
            try
            {
                ds = prs.dts(string_temp, Conn, ds);
                radGridView2.DataSource = ds.Tables[0];

                //MessageBox.Show(dataGridViewX4.Rows.Count.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();

            }
        }
        private void bt_Search_Click(object sender, EventArgs e)
        {
            BangKe();
        }

        private void bt_save_input_Click(object sender, EventArgs e)
        {
            save();
        }

        private void radGridView2_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView2);
        }
        private void Search_edit()
        {
            Conn = prs.Connect();
            Conn.Open();
            string[] temp = dateTimePicker3.Text.Split('/');
            Datebegin = temp[1] + "/" + temp[0] + "/" + temp[2];
            temp.Clone();
            string[] temp1 = dateTimePicker4.Text.Split('/');
            Dateend = temp1[1] + "/" + temp1[0] + "/" + temp1[2];
            temp1.Clone();
            string_temp = @"SELECT [Serial],KiemKe.TenSP,Load_CTSP_Mua.TenChiTiet as TenCT,[MaCT],[CDay],[CRong],[CDai],[SoLuong],[KhuVuc],[GhiChu],[Ngay]
                        FROM [Planning].[dbo].[KiemKe] left join DataBase_ScansiaPacific2013.dbo.Load_CTSP_Mua on [Planning].[dbo].[KiemKe].MaCT=DataBase_ScansiaPacific2013.dbo.Load_CTSP_Mua.MaChiTiet
                        where Ngay>='" + Datebegin + " 00:00' and Ngay<='" + Dateend + " 23:59'";
            if (cb_KhuVuc_edit.Text != "")
                string_temp += " and KhuVuc='" + cb_KhuVuc_edit.Text + "'";
            if (cb_MaSP_edit.Text != "")
                string_temp += " and MaCT like '" + prs.substring_right(cb_MaSP_edit.Text, child) + "%'";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandText = string_temp;
            //DataSet ds = new DataSet();
            try
            {
                if (radGridView4.Rows.Count > 0)
                    radGridView4.Rows.Clear();
                SqlDataReader rd = cmd.ExecuteReader();
                //MessageBox.Show(string_temp);
                while (rd.Read())
                {
                  
                    //cb_SanPham.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    radGridView4.Rows.Add(false,rd["Serial"].ToString(), rd["TenSP"].ToString(), rd["MaCT"].ToString(), rd["TenCT"].ToString(), rd["CDay"].ToString(), rd["CRong"].ToString(), rd["CDai"].ToString(), rd["SoLuong"].ToString(), rd["KhuVuc"].ToString(), rd["GhiChu"].ToString());

                }
                rd.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_search_edit_Click(object sender, EventArgs e)
        {
            Search_edit();
        }
        private void edit()
        {
            if (radGridView4.Rows.Count > 0)
            {
                Conn = prs.Connect();
                Conn.Open();
                string check = "";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                try
                {
                    DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa những thông tin được check phía dưới", "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        while (radGridView4.Rows.Count > 0)
                        {
                            check = radGridView4.Rows[0].Cells["Check"].Value.ToString();
                            if (check == "True")
                            {
                                Serial1 = radGridView4.Rows[0].Cells["Serial"].Value.ToString();
                                MaCT1 = radGridView4.Rows[0].Cells["MaCT"].Value.ToString();
                                CDay1 = radGridView4.Rows[0].Cells["CDay"].Value.ToString();
                                CRong1 = radGridView4.Rows[0].Cells["CRong"].Value.ToString();
                                CDai1 = radGridView4.Rows[0].Cells["CDai"].Value.ToString();
                                SoLuong1 = radGridView4.Rows[0].Cells["SoLuong"].Value.ToString();
                                KhuVuc1 = radGridView4.Rows[0].Cells["KhuVuc"].Value.ToString();
                                GhiChu1 = radGridView4.Rows[0].Cells["GhiChu"].Value.ToString();
                                string_temp = @"UPDATE [Planning].[dbo].[KiemKe] SET [MaCT] ='" + MaCT1 + "' ,[CDay] =" + CDay1 + " ,[CRong] =" + CRong1 + " ,[CDai] =" + CDai1 + ",[SoLuong] =" + SoLuong1 + " ,[KhuVuc] =N'" + KhuVuc1 + "' ,[GhiChu] =N'" + GhiChu1 + "' WHERE Serial=" + Serial1;
                                cmd.CommandText = string_temp;
                                cmd.ExecuteNonQuery();
                            }
                            radGridView4.Rows.RemoveAt(0);
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            edit();
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            if (radGridView4.Rows.Count > 0)
            {
                Conn = prs.Connect();
                Conn.Open();
                string check = "";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = Conn;
                try
                {
                    DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa những thông tin được check phía dưới", "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        while (radGridView4.Rows.Count > 0)
                        {
                            check = radGridView4.Rows[0].Cells["Check"].Value.ToString();
                            if (check == "True")
                            {
                                Serial1 = radGridView4.Rows[0].Cells["Serial"].Value.ToString();
                                string_temp = @"DELETE FROM [Planning].[dbo].[KiemKe]
                                            WHERE Serial=" + Serial1;
                                cmd.CommandText = string_temp;
                                cmd.ExecuteNonQuery();
                            }
                            radGridView4.Rows.RemoveAt(0);
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }

        private void bt_exportexcel_BK_Click(object sender, EventArgs e)
        {
            if (radGridView2.Rows.Count > 1)
            {
                DataTable dt = (DataTable)radGridView2.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\KiemKe.xlsx", 6, 1);
                // prs.ExportExcel(dt, @"\\192.168.1.10\SHARE\Book1.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }

        private void cb_KhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
